package ecs

type EntityManager struct {
	entities []*Entity
}

func NewEntityManager() *EntityManager {
	return &EntityManager{
		entities: []*Entity{},
	}
}

func (m *EntityManager) Add(entities ...*Entity) {
	m.entities = append(m.entities, entities...)
}

func (m *EntityManager) Remove(entity *Entity) {
	for i, e := range m.entities {
		if e.id == entity.id {
			m.entities = append(m.entities[:i], m.entities[i+1:]...)
			break
		}
	}
}
func (m *EntityManager) RemoveById(id EntityId) {
	for i, e := range m.entities {
		if e.id == id {
			m.entities = append(m.entities[:i], m.entities[i+1:]...)
			break
		}
	}
}

func (m *EntityManager) Get(id EntityId) (entity *Entity) {
	for _, e := range m.entities {
		if e.id == id {
			return e
		}
	}
	return
}

func (m *EntityManager) FilterByMask(mask uint64) (entities []*Entity) {
	entities = make([]*Entity, len(m.entities))
	index := 0
	for _, e := range m.entities {
		observed := e.mask
		if observed&mask == mask {
			entities[index] = e
			index++
		}
	}
	return entities[:index]
}

func (m *EntityManager) Entities() (entities []*Entity) {
	return m.entities
}
